﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;
using ncp.Business.Common;
using ncp.Business.FrendLink;

namespace ncp.Web
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof (index));
            if (!IsPostBack)
            {
                GetLatestNews(0);
                GetLatestNews(1);
                GetLatestNews(2);
                GetLatestNews(3);
                GetLeftNews("006");
                GetLeftNews("011");
                GetLeftNews("003");
                GetLeftNews("004");
                GetMembers("0");
                GetMembers("1");
                GetShowList();
                BindFLink();
            }
        }


        //获取最新的产品供求
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public void GetLatestNews(int type)
        {
             try
            {
                var dic = new Dictionary<string, object>();

                switch (type)
                {
                    case 0:
                        //获取前三个供应
                        dic.Clear();
                        dic.Add("Product_Status", "1");
                        var dtSupply = ProductInfoBll.GetProductInfoTable(" top 3 *", dic, "", CommonConst.DB_Name, true);
                        repFirst1.DataSource = dtSupply;
                        repFirst1.DataBind();
                        //后四个求购
                        dic.Clear();
                        dic.Add("NC_Code", "001");
                        dic.Add("News_Status", "1");
                        var dtBuy = NewsInfoBll.GetNewsInfoTable(" top 4 *", dic, "", CommonConst.DB_Name,true);
                        repnews1.DataSource = dtBuy;
                        repnews1.DataBind();
                        break;
                    case 1:
                        dic.Clear();
                        dic.Add("Product_Status", "1");
                        dtSupply = ProductInfoBll.GetProductInfoTable(" top 7 *", dic, "", CommonConst.DB_Name,true);
                        repProduct1.DataSource = dtSupply;
                        repProduct1.DataBind();
                        break;
                    case 2:
                        dic.Clear();
                        //dic.Add("NC_Code", "001");
                        dic.Add("more", "CHARINDEX('001',News_Code)<>0");
                        dic.Add("News_Status", "1");
                        dtBuy = NewsInfoBll.GetNewsInfoTable(" top 7 *", dic, "", CommonConst.DB_Name,true);
                        repnews2.DataSource = dtBuy;
                        repnews2.DataBind();
                        break;
                    case 3:
                        dic.Clear();
                        //dic.Add("NC_Code", "002");
                        dic.Add("more", "CHARINDEX('002',News_Code)<>0");
                        dic.Add("News_Status", "1");
                        dtBuy = NewsInfoBll.GetNewsInfoTable(" top 7 *", dic, "admin", CommonConst.DB_Name,true);
                        repnews3.DataSource = dtBuy;
                        repnews3.DataBind();
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        //行情价格
        public void GetLeftNews(string type)
        {
            try
            {
                var dic = new Dictionary<string, object>();
                //dic.Add("NC_Code", type);
                dic.Add("more", "CHARINDEX('" + type + "',News_Code)<>0");
                dic.Add("News_Status", "1");
                var dt = NewsInfoBll.GetNewsInfoTable(" top 7 *", dic, "admin", CommonConst.DB_Name,true);
                switch (type)
                {
                    case "006":
                        reppriceList.DataSource = dt;
                        reppriceList.DataBind();
                        break;
                    case "011":
                        repexpertList.DataSource = dt;
                        repexpertList.DataBind();
                        break;
                    case "003":
                        repscientificList.DataSource = dt;
                        repscientificList.DataBind();
                        break;
                    case "004":
                        reppolicyList.DataSource = dt;
                        reppolicyList.DataBind();
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //行业之星和推荐企业
        public void GetMembers(string type)
        {
            var dic = new Dictionary<string, object>();
            try
            {
                DataTable dt;
                switch (type)
                {
                        //行业之星
                    case "0":
                        dic.Clear();
                        dic.Add("User_Status", "1");
                        dic.Add("more", "CHARINDEX('a',User_Flag)<>0");
                        dt = ClientInfoBll.GetClientInfoTable("top 7 *", dic, CommonConst.DB_Name);
                        repStar.DataSource = dt;
                        repStar.DataBind();
                        break;
                        //推荐企业
                    case "1":
                        dic.Clear();
                        dic.Add("User_Status", "1");
                        dic.Add("more", "CHARINDEX('c',User_Flag)<>0");
                        dt = ClientInfoBll.GetClientInfoTable("top 7 *", dic, CommonConst.DB_Name);
                        repRecClient.DataSource = dt;
                        repRecClient.DataBind();
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //推荐产品
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetRecProduct()
        {
            var jsonResult = new Dictionary<string, object>();
            var dic = new Dictionary<string, object>();
            dic.Add("Product_Status", "1");
            dic.Add("more", "CHARINDEX('h',Product_Flag)<>0");
            try
            {
                var dt = ProductInfoBll.GetProductInfoTable(" top 5 *", dic, "", CommonConst.DB_Name,false);
                var builder = new StringBuilder();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    builder.AppendFormat(
                        "<dl><dt><a href=\"/product/{0}.html\" target=\"_blank\"><img src=\"{1}\" width=\"50\" height=\"50\" /></a></dt>",
                        EncryptHelper.EncryptString(dt.Rows[i]["Product_Id"].ToString()), dt.Rows[i]["Product_Picture"]);
                    builder.AppendFormat(
                        "<dd class=\"protitle\"><a href=\"/product/{0}.html\" target=\"_blank\">{1}</a></dd>",
                        EncryptHelper.EncryptString(dt.Rows[i]["Product_Id"].ToString()), dt.Rows[i]["Product_Title"]);
                    builder.Append("<dd class=\"proprice\"><span> </span><b></b></dd>");
                    string style;
                    if (i == 0 || i == 1)
                    {
                        style = "hottop";
                    }
                    else if (i == 2)
                    {
                        style = "hotcen";
                    }
                    else
                    {
                        style = "hotbot";
                    }
                    builder.AppendFormat("<dd class=\"pro-num {0}\">{1}</dd>", style, i+1);
                    builder.Append("<div class=\"bh-clear\"></div></dl>");
                }
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", builder.ToString());
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", ex.Message);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //登录
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string UserLogin(string account, string pwd)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                //判断用户是否已经登录
                var userInfo = WebMasterCookie.GetCookie();
                if (userInfo != null)
                {
                    jsonResult.Add("isreload", true);
                    jsonResult.Add("msg", "/member/member_information.html");
                }
                else
                {
                    DataTable table = ClientInfoBll.ClientLogin(CommonBll.InputText(account, 50),
                                                                EncryptHelper.EncryptString(pwd), CommonConst.DB_Name);
                    if (table.Rows.Count > 0)
                    {
                        //判断当天是否已经登录过
                        if (table.Rows[0]["LastLoginTime"] == DBNull.Value ||
                            (Convert.ToDateTime(table.Rows[0]["LastLoginTime"].ToString()) - DateTime.Now).Days != 0)
                        {
                            //登录时积分+5并更新等级
                            ClientInfoBll.UpdateClientPoints(table.Rows[0]["Client_Id"].ToString(),
                                                             CommonConst.gLoginPoints, "+", CommonConst.DB_Name);
                            //更新最后登录时间
                            ClientInfoBll.UpateLastLoginTime(table.Rows[0]["Client_Id"].ToString(), CommonConst.DB_Name);
                        }

                        jsonResult.Add("isreload", true);
                        var info = new UserInfo
                                       {
                                           Email =
                                               table.Rows[0]["User_Email"] == DBNull.Value
                                                   ? ""
                                                   : table.Rows[0]["User_Email"].ToString(),
                                           Id = table.Rows[0]["Client_Id"].ToString(),
                                           RelName =
                                               table.Rows[0]["Client_Name"] == DBNull.Value
                                                   ? ""
                                                   : table.Rows[0]["Client_Name"].ToString(),
                                           Status = table.Rows[0]["User_Status"].ToString(),
                                           UName = table.Rows[0]["Client_Account"].ToString()
                                       };
                        info.Computername = HttpContext.Current.Application["computername"].ToString();
                        info.Computerip = HttpContext.Current.Application["computerip"].ToString();
                        WebMasterCookie.AddCookie(info);

                        Dictionary<string, object> dicLoginLog = new Dictionary<string, object>();
                        dicLoginLog.Add("user_id", info.Id);
                        dicLoginLog.Add("computername", info.Computername);
                        dicLoginLog.Add("computerip", info.Computerip);
                        LogLoginBll.AddLoginLog(dicLoginLog, CommonConst.DB_Name);
                        jsonResult.Add("msg", "/member/member_information.html");
                    }
                    else
                    {
                        jsonResult.Clear();
                        jsonResult.Add("isreload", false);
                        jsonResult.Add("msg", "用户名或密码错误！");
                    }
                }

            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", ex.Message);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";

        }

        //个人信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetUserInfo()
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            var userInfo = WebMasterCookie.GetCookie();
            StringBuilder builder1 = new StringBuilder();
            builder1.Append("<div class=\"username\"><span>用户名：</span><input id=\"textAccount\" type=\"text\" value=\"\" /></div>");
            builder1.Append("<div class=\"password\"><span>密　码：</span><input id=\"textPwd\" type=\"password\" value=\"\" /></div>");
            builder1.Append("<div class=\"submit\"><a href=\"/register.html\" class=\"zc\">免费注册</a><input type=\"button\" onclick=\"login()\"  value=\"登　录\" /><a href=\"/forget_passwd.html\" class=\"forget\">忘记密码？</a>");
            builder1.Append("<div class=\"bh-clear\"></div></div>");
            if (userInfo != null)
            {
                var table = ClientInfoBll.GetClientInfoTable("Client_Id=" + userInfo.Id, "Client_Id asc", CommonConst.DB_Name);
                if (table.Rows.Count == 0)
                {
                    jsonResult.Add("msg", builder1.ToString());
                    WebMasterCookie.DeleteCookie();
                }
                else
                {
                    StringBuilder builder = new StringBuilder();
                    builder.AppendFormat("<div class=\"acc\">欢迎：{0}</div>", userInfo.UName);
                    builder.AppendFormat("<div class=\"acc\">积分：<span>{0}</span>&nbsp;&nbsp;等级：<span>{1}</span></div>", table.Rows[0]["User_Points"], ClientInfoBll.GetClientLevel(table.Rows[0]["User_Points"].ToString(), CommonConst.DB_Name));
                    builder.AppendFormat("<div class=\"acc\" style=\"border-bottom: 1px dashed #CCC;\"><a href=\"/member/message/index.html\">[个人中心]</a><a href=\"/shop{0}/about.html\">[我的商铺]</a><a href=\"javascript:void(0)\" onclick=\"loginOut()\">[退出]</a></div>",
                        CommonBll.BuildShopId(Convert.ToInt32(userInfo.Id)));
                    jsonResult.Add("msg", builder.ToString());
                }

            }
            else
            {
                
                jsonResult.Add("msg",builder1.ToString());
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //行业展会
        public void GetShowList()
        {
            try
            {
                var dic = new Dictionary<string, object>();
                dic.Add("News_Status", "1");
                //dic.Add("NC_Code", "005");
                dic.Add("more", "CHARINDEX('005',News_Code)<>0");
                var dt = NewsInfoBll.GetNewsInfoTable(" top 7 *", dic, "admin", CommonConst.DB_Name, false);
                repShowList.DataSource = dt;
                repShowList.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //友情链接
        private void BindFLink()
        {
            Dictionary<string, object> dicWhere = new Dictionary<string, object>();
            dicWhere.Add("Link_Status", "1");
            var fLink = FrendLinkBll.GetFrendLinkTable("*", dicWhere, "order by Link_Order", CommonConst.DB_Name);
            repLink.DataSource = fLink;
            repLink.DataBind();
        }

        //广告
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetADList()
        {
            var dic = new Dictionary<string, object>();
            dic.Add("ad11_1", "<img src=\"../image/private/pic-2.jpg\" width=\"243px\" height=\"46px\" />");

            dic.Add("ad4_1", "<img src=\"../image/private/pic-4.jpg\" width=\"240px\" height=\"85px\" />");
            dic.Add("ad4_2", "<img src=\"../image/private/pic-5.jpg\" width=\"240px\" height=\"85px\" />");
            dic.Add("ad4_3", "<img src=\"../image/private/pic-6.jpg\" width=\"240px\" height=\"85px\" />");
            dic.Add("ad4_4", "<img src=\"../image/private/pic-7.jpg\" width=\"240px\" height=\"85px\" />");
            dic.Add("ad4_5", "<img src=\"../image/private/pic-4.jpg\" width=\"240px\" height=\"85px\" />");
            dic.Add("ad4_6", "<img src=\"../image/private/pic-5.jpg\" width=\"240px\" height=\"85px\" />");
            dic.Add("ad4_7", "<img src=\"../image/private/pic-6.jpg\" width=\"240px\" height=\"85px\" />");
            dic.Add("ad4_8", "<img src=\"../image/private/pic-7.jpg\" width=\"240px\" height=\"85px\" />");

            dic.Add("ad5_1", "<img src=\"../image/private/pic-3.jpg\" width=\"1003px\" height=\"85px\" />");
            dic.Add("ad5_2", "<img src=\"../image/private/pic-3.jpg\" width=\"1003px\" height=\"85px\" />");

            dic.Add("ad6_1", "<img src=\"../image/private/pic-8.jpg\" width=\"204px\" height=\"60px\" />");
            dic.Add("ad6_2", "<img src=\"../image/private/pic-8.jpg\" width=\"204px\" height=\"60px\" />");
            dic.Add("ad6_3", "<img src=\"../image/private/pic-8.jpg\" width=\"204px\" height=\"60px\" />");
            dic.Add("ad6_4", "<img src=\"../image/private/pic-8.jpg\" width=\"204px\" height=\"60px\" />");
            dic.Add("ad6_5", "<img src=\"../image/private/pic-8.jpg\" width=\"204px\" height=\"60px\" />");
            dic.Add("ad6_6", "<img src=\"../image/private/pic-8.jpg\" width=\"204px\" height=\"60px\" />");

            dic.Add("ad7_1", "<img src=\"../image/private/pic-10.jpg\" width=\"92px\" height=\"48px\" />");
            dic.Add("ad7_2", "<img src=\"../image/private/pic-10.jpg\" width=\"92px\" height=\"48px\" />");
            dic.Add("ad7_3", "<img src=\"../image/private/pic-10.jpg\" width=\"92px\" height=\"48px\" />");
            dic.Add("ad7_4", "<img src=\"../image/private/pic-10.jpg\" width=\"92px\" height=\"48px\" />");
            dic.Add("ad7_5", "<img src=\"../image/private/pic-10.jpg\" width=\"92px\" height=\"48px\" />");
            dic.Add("ad7_6", "<img src=\"../image/private/pic-10.jpg\" width=\"92px\" height=\"48px\" />");
            dic.Add("ad7_7", "<img src=\"../image/private/pic-10.jpg\" width=\"92px\" height=\"48px\" />");
            dic.Add("ad7_8", "<img src=\"../image/private/pic-10.jpg\" width=\"92px\" height=\"48px\" />");

            dic.Add("ad10_1", "<img src=\"../image/private/pic-11.jpg\" width=\"214px\" height=\"146px\" />");

            var dt = CommonBll.GetdtList("select * from ads where adid>1 and  endtime>=getdate() order by adid,sort", CommonConst.DB_Name);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                switch (dt.Rows[i]["adid"].ToString())
                {
                    case "11"://首页243*46  
                        if(string.IsNullOrEmpty(dt.Rows[i]["url"].ToString()))
                            dic["ad11_1"] = string.Format("<img src=\"{0}\" width=\"{1}\" height=\"{2}\" title=\"{3}\" />",  dt.Rows[i]["picture"].ToString(), dt.Rows[i]["width"].ToString(), dt.Rows[i]["height"].ToString(),dt.Rows[i]["content"].ToString());
                        else
                            dic["ad11_1"] = string.Format("<a href=\"{0}\" target=\"_blank\" title=\"{1}\"><img src=\"{2}\" width=\"{3}\" height=\"{4}\" /></a>", dt.Rows[i]["url"].ToString(), dt.Rows[i]["content"].ToString(), dt.Rows[i]["picture"].ToString(), dt.Rows[i]["width"].ToString(), dt.Rows[i]["height"].ToString());
                        break;
                    case "4"://首页240*85 
                        if (string.IsNullOrEmpty(dt.Rows[i]["url"].ToString()))
                        {
                            if ("1,2,3,4,5,6,7,8".Contains(dt.Rows[i]["sort"].ToString()))
                                dic["ad4_" + dt.Rows[i]["sort"].ToString()] = string.Format("<img src=\"{0}\" width=\"{1}\" height=\"{2}\" title=\"{3}\" />", dt.Rows[i]["picture"].ToString(), dt.Rows[i]["width"].ToString(), dt.Rows[i]["height"].ToString(), dt.Rows[i]["content"].ToString());
                        }
                        else
                        {
                            if ("1,2,3,4,5,6,7,8".Contains(dt.Rows[i]["sort"].ToString()))
                                dic["ad4_" + dt.Rows[i]["sort"].ToString()] = string.Format("<a href=\"{0}\" target=\"_blank\" title=\"{1}\"><img src=\"{2}\" width=\"{3}\" height=\"{4}\" /></a>", dt.Rows[i]["url"].ToString(), dt.Rows[i]["content"].ToString(), dt.Rows[i]["picture"].ToString(), dt.Rows[i]["width"].ToString(), dt.Rows[i]["height"].ToString());
                        }
                        break;
                    case "5"://首页1003*85 
                        if (string.IsNullOrEmpty(dt.Rows[i]["url"].ToString()))
                        {
                            if ("1,2".Contains(dt.Rows[i]["sort"].ToString()))
                                dic["ad5_" + dt.Rows[i]["sort"].ToString()] = string.Format("<img src=\"{0}\" width=\"{1}\" height=\"{2}\" title=\"{3}\" />", dt.Rows[i]["picture"].ToString(), dt.Rows[i]["width"].ToString(), dt.Rows[i]["height"].ToString(), dt.Rows[i]["content"].ToString());
                        }
                        else
                        {
                            if ("1,2".Contains(dt.Rows[i]["sort"].ToString()))
                                dic["ad5_" + dt.Rows[i]["sort"].ToString()] = string.Format("<a href=\"{0}\" target=\"_blank\" title=\"{1}\"><img src=\"{2}\" width=\"{3}\" height=\"{4}\" /></a>", dt.Rows[i]["url"].ToString(), dt.Rows[i]["content"].ToString(), dt.Rows[i]["picture"].ToString(), dt.Rows[i]["width"].ToString(), dt.Rows[i]["height"].ToString());
                        }
                        break;
                    case "6"://204*60
                        if (string.IsNullOrEmpty(dt.Rows[i]["url"].ToString()))
                        {
                            if ("1,2,3,4,5,6".Contains(dt.Rows[i]["sort"].ToString()))
                                dic["ad6_" + dt.Rows[i]["sort"].ToString()] = string.Format("<img src=\"{0}\" width=\"{1}\" height=\"{2}\" title=\"{3}\" />", dt.Rows[i]["picture"].ToString(), dt.Rows[i]["width"].ToString(), dt.Rows[i]["height"].ToString(), dt.Rows[i]["content"].ToString());
                        }
                        else
                        {
                            if ("1,2,3,4,5,6".Contains(dt.Rows[i]["sort"].ToString()))
                                dic["ad6_" + dt.Rows[i]["sort"].ToString()] = string.Format("<a href=\"{0}\" target=\"_blank\" title=\"{1}\"><img src=\"{2}\" width=\"{3}\" height=\"{4}\" /></a>", dt.Rows[i]["url"].ToString(), dt.Rows[i]["content"].ToString(), dt.Rows[i]["picture"].ToString(), dt.Rows[i]["width"].ToString(), dt.Rows[i]["height"].ToString());
                        }
                        break;
                    case "7"://首页92*48   
                        if (string.IsNullOrEmpty(dt.Rows[i]["url"].ToString()))
                        {
                            if ("1,2,3,4,5,6,7,8".Contains(dt.Rows[i]["sort"].ToString()))
                                dic["ad7_" + dt.Rows[i]["sort"].ToString()] = string.Format("<img src=\"{0}\" width=\"{1}\" height=\"{2}\" title=\"{3}\" />", dt.Rows[i]["picture"].ToString(), dt.Rows[i]["width"].ToString(), dt.Rows[i]["height"].ToString(), dt.Rows[i]["content"].ToString());
                        }
                        else
                        {
                            if ("1,2,3,4,5,6,7,8".Contains(dt.Rows[i]["sort"].ToString()))
                                dic["ad7_" + dt.Rows[i]["sort"].ToString()] = string.Format("<a href=\"{0}\" target=\"_blank\" title=\"{1}\"><img src=\"{2}\" width=\"{3}\" height=\"{4}\" /></a>", dt.Rows[i]["url"].ToString(), dt.Rows[i]["content"].ToString(), dt.Rows[i]["picture"].ToString(), dt.Rows[i]["width"].ToString(), dt.Rows[i]["height"].ToString());
                        }
                        break;
                    case "10"://产品214*146 
                        if (string.IsNullOrEmpty(dt.Rows[i]["url"].ToString()))
                        {
                            if ("1".Contains(dt.Rows[i]["sort"].ToString()))
                                dic["ad10_1"] = string.Format("<img src=\"{0}\" width=\"{1}\" height=\"{2}\" title=\"{3}\" />", dt.Rows[i]["picture"].ToString(), dt.Rows[i]["width"].ToString(), dt.Rows[i]["height"].ToString(), dt.Rows[i]["content"].ToString());
                        }
                        else
                        {
                            if ("1".Contains(dt.Rows[i]["sort"].ToString()))
                                dic["ad10_1"] = string.Format("<a href=\"{0}\" target=\"_blank\" title=\"{1}\"><img src=\"{2}\" width=\"{3}\" height=\"{4}\" /></a>", dt.Rows[i]["url"].ToString(), dt.Rows[i]["content"].ToString(), dt.Rows[i]["picture"].ToString(), dt.Rows[i]["width"].ToString(), dt.Rows[i]["height"].ToString());
                        }
                        break;
                }

            }
            return "[" + JsonHelper.ObjectToJSON(dic) + "]";
        }
    }
}
